﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Shop.Wares.Repositories;
using Shop.Wares.Models;
using PetaPoco;

namespace Shop.Wares.Services
{
    public class WaresAccess
    {
        Repositories.WaresAccess dal = new Repositories.WaresAccess();
        #region 类别子类别管理
        /// <summary>
        /// 验证类别名是否存在
        /// </summary>
        /// <param name="CName">类别名</param>
        /// <returns></returns>
        public int VerificationCName(string CName)
        {
            WaresCategory winfo = dal.VerificationCName(CName);
            //如果为空的话说明不存在
            if (winfo == null)
            {
                return 1;
            }
            return 0;
        }
        /// <summary>
        /// 修改类别时验证类别名存不存在
        /// </summary>
        /// <param name="CName">类别名</param>
        /// <returns></returns>
        public int UVerificationCName(string CName,string YCName)
        {
            WaresCategory winfo = dal.VerificationCName(CName);
            //如果为空的话说明不存在
            if (winfo == null)
            {
                return 1;
            }
            //如果输入的和原来的一样的话是可以的
            else if (winfo.CName == YCName)
            {
                return 1;
            }
            return 0;
        }
        /// <summary>
        /// 添加类别
        /// </summary>
        /// <param name="winfo"></param>
        /// <returns></returns>
        public object AddWaresCategory(WaresCategory winfo)
        {
            return dal.AddWaresCategory(winfo);
        }
        /// <summary>
        /// 获取类别信息
        /// </summary>
        /// <returns></returns>
        public IList<WaresCategory> GetWinfo()
        {
            return dal.GetWinfo();
        }

        public List<WaresSale> GetWaresSale(string value = "",int page = 0,int limit = 0)
        {
            return dal.GetWaresSale(value,page,limit);
        }

        public int GetCount()
        {
            return dal.GetCount();
        }
        
        /// <summary>
        /// 删除类别
        /// </summary>
        /// <param name="CID">类别ID</param>
        /// <returns></returns>
        public int DelWaresCategory(int CID)
        {
            return dal.DelWaresCategory(CID);
        }
        /// <summary>
        /// 修改类别
        /// </summary>
        /// <param name="winfo">类别ID</param>
        /// <returns></returns>
        public object UpdateWaresCategory(WaresCategory winfo)
        {
            return dal.UpdateWaresCategory(winfo);
        }
        /// <summary>
        /// 判断类别下子类别数量
        /// </summary>
        /// <param name="CID">类别ID</param>
        /// <returns>没有子类别返回1，有子类别返回0</returns>
        public int SWCount(int CID)
        {
            IList<SonWaresCategory> swinfo = dal.GetSWinfo(CID);
            if (swinfo.Count == 0)
            {
                return 1;
            }
            return 0;
        }
        /// <summary>
        /// 置顶类别
        /// </summary>
        /// <param name="CID">类别ID</param>
        /// <returns></returns>
        public int OrderTimeWaresCategory(int CID)
        {
            return dal.OrderTimeWaresCategory(CID);
        }



        /// <summary>
        /// 根据类别ID获取子类别信息
        /// </summary>
        /// <param name="CID">类别ID</param>
        /// <returns></returns>
        public IList<SonWaresCategory> GetSWInfo(int CID)
        {
            return dal.GetSWinfo(CID);
        }
        /// <summary>
        /// 验证子类别名是否存在
        /// </summary>
        /// <param name="CName">子类别名</param>
        /// <returns></returns>
        public int VerificationSCName(string SCName)
        {
            SonWaresCategory winfo = dal.VerificationSCName(SCName);
            //如果为空的话说明不存在
            if (winfo == null)
            {
                return 1;
            }
            return 0;
        }
        /// <summary>
        /// 修改类别时验证子类别名存不存在
        /// </summary>
        /// <param name="CName">类别名</param>
        /// <returns></returns>
        public int UVerificationSCName(string SCName,string YSCName)
        {
            SonWaresCategory swinfo = dal.VerificationSCName(SCName);
            //如果为空的话说明不存在
            if (swinfo == null)
            {
                return 1;
            }
            //如果输入的和原来的一样的话是可以的
            else if (swinfo.SCName == YSCName)
            {
                return 1;
            }
            return 0;
        }
        /// <summary>
        /// 添加类别
        /// </summary>
        /// <param name="winfo">类别信息</param>
        /// <returns></returns>
        public object AddSonWaresCategory(SonWaresCategory swinfo)
        {
            return dal.AddSonWaresCategory(swinfo);
        }
        /// <summary>
        /// 删除子类别
        /// </summary>
        /// <param name="SCID">子类别ID</param>
        /// <returns></returns>
        public int DelSonWaresCategory(int SCID)
        {
            return dal.DelSonWaresCategory(SCID);
        }
        /// <summary>
        /// 修改子类别
        /// </summary>
        /// <param name="swinfo">子类别信息</param>
        /// <returns></returns>
        public object UpdateSonWaresCategory(SonWaresCategory swinfo)
        {
            return dal.UpdateSonWaresCategory(swinfo);
        }
        /// <summary>
        /// 置顶子类别
        /// </summary>
        /// <param name="SCID">子类别ID</param>
        /// <returns></returns>
        public int OrderTimeSonWaresCategory(int SCID)
        {
            return dal.OrderTimeSonWaresCategory(SCID);
        }
        #endregion

        #region 商品管理
        /// <summary>
        ///分页获取商品信息 
        /// </summary>
        /// <param name="page">第几页</param>
        /// <param name="itemsPerPage">每页多少行</param>
        /// <returns></returns>
        public Page<Models.AnyWares> GetWaresInfo(long page, long itemsPerPage)
        {
            return dal.GetWaresInfo(page, itemsPerPage);
        }
        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="winfo"></param>
        /// <returns></returns>
        public object AddWares(Models.Wares winfo)
        {
            return dal.AddWares(winfo);
        }
        /// <summary>
        /// 根据商品ID获取商品信息
        /// </summary>
        /// <param name="WID"></param>
        /// <returns></returns>
        public Models.Wares GetWaresInfo(int WID)
        {
            return dal.GetWaresInfo(WID);
        }
        /// <summary>
        /// 修改商品信息
        /// </summary>
        /// <param name="winfo"></param>
        /// <returns></returns>
        public int UpdateWares(Models.Wares winfo)
        {
            return dal.UpdateWares(winfo);
        }
        /// <summary>
        /// 置顶商品
        /// </summary>
        /// <param name="WID">商品ID</param>
        /// <returns></returns>
        public int OrderTimeWares(int WID)
        {
            return dal.OrderTimeWares(WID);
        }
        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="WID"></param>
        /// <returns></returns>
        public int DelWares(int WID)
        {
            return dal.DelWares(WID);
        }
        /// <summary>
        ///分页获取搜索的商品信息 
        /// </summary>
        /// <param name="page">第几页</param>
        /// <param name="itemsPerPage">每页多少行</param>
        /// <param name="CID">类别ID</param>
        /// <param name="SCID">子类别Id</param>
        /// <param name="WName">商品名</param>
        /// <param name="Status">商品状态</param>
        /// <returns></returns>
        public Page<Models.AnyWares> GetSerchWaresInfo(long page, long itemsPerPage, int CID, int SCID, string WName,int Status)
        {
            return dal.GetSerchWaresInfo(page,itemsPerPage,CID,SCID,WName, Status);
        }

        /// <summary>
        /// 上下架0上架1下架
        /// </summary>
        /// <param name="Type"></param>
        /// <param name="WID"></param>
        /// <returns></returns>
        public int UDFrame(int Type, int WID)
        {
            return dal.UDFrame(Type,WID);
        }
        #endregion
    }
}
